import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

@SuppressWarnings({"all"})
class MyThreadPool {
    // 1. 描述一个任务
    // 2. 使用一个数据结构来组织若干个任务
    private BlockingDeque<Runnable> queue = new LinkedBlockingDeque<>();

    // 3. 描述一个线程
    static class Worker extends Thread {
        private BlockingDeque<Runnable> queue = null;

        public Worker(BlockingDeque<Runnable> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            // 需要能够拿到上面的队列
            while (true) {
                try {
                    // 循环获取任务队列的任务
                    Runnable runnable = queue.take();
                    // 获取之后，就执行任务
                    runnable.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private List<Thread> workers = new ArrayList<>();

    public MyThreadPool(int n) {
        for (int i = 0; i < n; i++) {
            Worker worker = new Worker(queue);
            worker.start();
            workers.add(worker);
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool = new MyThreadPool(10);
        for (int i = 0; i < 100; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello threadpool");
                }
            });
        }
    }
}
